package com.igeek.ch02.controller;

import com.igeek.ch02.common.CommonResult;
import com.igeek.ch02.utils.JSONUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet(name = "UserLogoutServlet",urlPatterns = "/logout")
public class UserLogoutServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //销毁会话
        HttpSession session = request.getSession();
        session.invalidate();

        //响应json数据
        CommonResult commonResult = new CommonResult(200 , "登出成功");
        JSONUtils.writeJson(response , commonResult);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        this.doPost(request,response);
    }
}
